import { defineStore } from 'pinia'
import { Part } from '@/types/system/device-maintenance/part'
import { useDeviceMaintenanceProjectStore } from './project'
import { getNodeIdsAsync } from '@/api/axios/device-server-client'
import * as api from '@/api/axios/device-maintenance-client'
import { ref, computed, watch } from 'vue'
import { ShowDeleteMessageBoxAsync } from '@/utils/message-box'
import { ComeGroup } from '@/types/system/device-maintenance/come-group'
import { ElMessage } from 'element-plus'

export const useDeviceMaintenanceComeGroupStore = defineStore(
  'device-maintenance-come-group',
  () => {
    const isLoading = ref(false)
    const currentProjectName = ref('')
    const isSelectProjectName = ref(false)
    const isLoadComeGroup = ref(false)
    const comeGroupIsSearched = ref(false)
    const hasComeGroup = ref(false)
    const projectNames = computed(() => {
      const store = useDeviceMaintenanceProjectStore()
      return store.projectNames
    })
    const currentComeGroup = ref<ComeGroup>(new ComeGroup())

    watch(currentProjectName, () => {
      updateComeGroupAsync()
    })

    function openLoading() {
      isLoading.value = true
    }
    function closeLoading() {
      isLoading.value = false
    }
    // 更新 COME 群组
    async function updateComeGroupAsync() {
      const store = useDeviceMaintenanceProjectStore()
      openLoading()
      if (store.isNeedLoadProjects) {
        const result = await store.updateProjectsAsync()
        if (!result) {
          ElMessage.warning('更新项目列表错误')
        }
      }
      const projectId = tryGetCurrentProjectId()
      if (projectId != null) {
        const result = await api.getComeGroupAsync(projectId)
        if (result.isSuccess) {
          comeGroupIsSearched.value = true
          if (result.result != null) {
            currentComeGroup.value = result.result
            hasComeGroup.value = true
          } else {
            hasComeGroup.value = false
          }
        } else {
          ElMessage.warning('获取项目群组错误')
        }
      }
      isLoadComeGroup.value = true
      closeLoading()
    }

    // 删除 COME 群组
    async function deleteComeGroupAsync() {
      openLoading()
      if (hasComeGroup.value) {
        const result = await api.deleteComeGroupAsync(currentComeGroup.value.id)
        if (result.isSuccess) {
          await updateComeGroupAsync()
        } else {
          ElMessage.warning('删除项目群组错误')
        }
      }
      closeLoading()
    }

    // 插入 COME 群组
    async function insertComeGroupAsync(comeGroup: ComeGroup) {
      openLoading()
      const projectId = tryGetCurrentProjectId()
      if (projectId != null) {
        comeGroup.projectId = projectId
        const result = await api.insertComeGroupAsync(comeGroup)
        if (result.isSuccess) {
          await updateComeGroupAsync()
        } else {
          ElMessage.warning('设置项目群组错误')
        }
      }
      closeLoading()
    }

    function tryGetCurrentProjectId() {
      const store = useDeviceMaintenanceProjectStore()
      const currentProject = store.projects.find(
        (item) => item.projectName == currentProjectName.value
      )
      return currentProject?.id ?? null
    }

    return {
      comeGroupIsSearched,
      hasComeGroup,
      isLoadComeGroup,
      isLoading,
      currentProjectName,
      currentComeGroup,
      isSelectProjectName,
      projectNames,
      updateComeGroupAsync,
      deleteComeGroupAsync,
      insertComeGroupAsync
    }
  }
)
